import hatch_interface

options = {}    # Only need to set non-default options.  See hatch_interface.optionDefaults for valid keys.
options["xmlFile"] = 's3board_hatch.xml'
options["bus"] = 'dcom_apb'
options["historyFile"] = __file__
#options["port"] = optionDefaults["port"]
if True:
    options["loggingLevel"] = 'info'
if False:
    options["bus"] = 'dummy'


hi = hatch_interface.setup_interface(options)   # XXX Create the interface object.


HIT_ENTER = "Hit enter to continue."


# Demonstrate node attributes (same line = same thing.)
if True:
    print hi.s3board.led.name, str(hi.s3board.led)  # 'name' attribute.
    print [node.name for node in hi.s3board.childNodes] # 'childNodes' attribute.
    print [node.name for node in hi.s3board] # __iter__().
    print str(hi.s3board.led.parent), str(hi.s3board)   # 'parent' attribute.
    print hi.s3board.led.properties     # 'properties' attribute.
    print hi.s3board.led.hatchFacade, hi    # 'hatchFacade' attribute.
    raw_input(HIT_ENTER)

# Demonstrate put() & get() functions.
if True:
    hi.put(0x22)    # Send write opcode...
    hi.put(0x00,0x00,0x00,0x08, 0x00,0x00,0x00,0x1C)  # (Write...) 1C to address 8.
    hi.put([0x21, 0x00,0x00,0x00,0x08])   # Read value in address 8 (brackets optional.)
    print hex(hi.get(4))    # Get and print the returned value from the interface.
    raw_input(HIT_ENTER)
    hi.put(*[0x22, 0x00,0x00,0x00,0x08, 0x00,0x00,0x00,0x00])  # Write 0x00 to address 8 (*args list optional.)

# Demonstrate read() & write() functions.
if True:
    print hex(hi.s3board.led.read()) # Read 'led' register.
    print hi.s3board.led()  # Read 'led' register (shorthand.)
    hi.s3board.led.write(0xFF)  # Write 0xFF to 'led' register.
    raw_input(HIT_ENTER)
    hi.s3board.led(0x00)    # Write 0x00 to 'led' register (shorthand.)

# Demonstrate iterators.
if True:
    for node in hi.s3board.depthFirst():    # Depth first.
        print node
    print [str(node) for node in hi.s3board.breadthFirst()] # Breadth first.
    raw_input(HIT_ENTER)

# Demonstrate recursive reads & writes.
if True:
    print [hex(value) for value in hi.s3board.cPack.readDepthFirst()]
    hi.s3board.cPack.writeDepthFirst(0xC)   # Single value 
    print [hex(value) for value in hi.s3board.cPack.readBreadthFirst()]
    hi.s3board.cPack.writeDepthFirst(0xA,0xB,0xC,0xD,0xE,0xF)   # Multiple values (extra values are ignored.)
    hi.s3board.cPack.writeDepthFirst([0xD,0xC,0xB]) # Brackets optional (values are always written to child Nodes until we run out of values.)
    raw_input(HIT_ENTER)
    hi.s3board.cPack.writeBreadthFirst(*[0x0,0x0,0x0,0x0])  # *args list optional.

# Demonstrate brackets.
if True:
    print hi.s3board.cPack, hi.s3board['cPack'], hi.s3board[3]   # These are all the same thing.
    hi.s3board.cPack[0].write(0x5)  # Write to first child node of cPack.
    print hex(hi.s3board[3][0].read())   # Multi-dimensional arrays also work.
    print hi.s3board.led, hi.s3board['led'], hi.s3board[2]  # These are all the same thing.
    print ", ".join([node.name for node in hi.s3board['button':'led']]) # Child nodes indexed by name (stops AT 'led'.)
    print ", ".join([node.name for node in hi.s3board[0:2]])    # Child nodes indexed by position in childNodes list (stops BEFORE 'led'.)
    print ", ".join([node.name for node in hi.s3board[:]])  # Index all child nodes.
    print ", ".join([node.name for node in hi.s3board[1:]]) # Partial index by integer.
    print ", ".join([node.name for node in hi.s3board[:'led']]) # Partial index by name.
    for node in hi.s3board: # Iterate through child nodes only, do not recurse.
        if len(node.childNodes) == 0:
            node.write(0xFF)
        else:
            print "Skipping " + ", ".join([cn.name for cn in node.childNodes])
    raw_input(HIT_ENTER)

# Demonstrate reading of nodes for which all child nodes share the same address.
if True:
    hi.s3board.cPack(0x1234)
    print hex(hi.s3board.cPack())


